package service;

import java.util.ArrayList;
import java.util.StringTokenizer;

import dataAccess.DataAccess;

/**
 * This provide some methods to read and write file, and read from database, and
 * return a list that contain all available word in dictionary read from
 * database.txt.
 */
public class Service {

        private ArrayList<String> listWord = new ArrayList<String>();
        private DataAccess da = new DataAccess();

        public Service() {
                listWord = da.getListWord();
        }
        

        /**
         * Check if the input word is Exist in dictionary or not
         *
         * @param word
         * @return
         */
        public boolean isExistWord(String word) {
                for (String temp : listWord) {
                        if (temp.toUpperCase().equals(word.toUpperCase())) {
                                return true;
                        }
                }
                return false;
        }

        /**
         * Read help file from file help.txt and return the document of read
         * file
         *
         * @return
         */
        public String helpText() {
                return da.readFileUTF8("src/dataaccess/Help form.txt");
        }

        /**
         * Read statics introduction from a file
         *
         * @return Document of that file
         */
        public String staticsText() {
                return da.readFileUTF8("src/dataAccess/Statics Info.txt");
        }

        /**
         * Read control introduction from a file
         *
         * @return Document of that file
         */
        public String controlsText() {
                return da.readFileUTF8("src/dataAccess/Controls Info.txt");
        }

        /**
         * This method will return a list information of statics form.
         *
         * @return
         */
        public String[] gameStatics() {
                String[] list = new String[8];
                StringTokenizer token = new StringTokenizer(da.readFile("src/dataAccess/GameStatics.txt"), "\n");
                for (int i = 0; i < list.length; i++) {
                        String temp = token.nextToken();
                        StringTokenizer token1 = new StringTokenizer(temp);
                        token1.nextToken();
                        list[i] = token1.nextToken();
                }
                return list;
        }

        /**
         * This method will return a list value of control keys
         *
         * @return
         */
        public int[] gameControls() {
                int[] list = new int[7];
                StringTokenizer token = new StringTokenizer(da.readFile("src/dataAccess/GameControls.txt"), "\n");
                StringTokenizer token1;
                for (int i = 0; i < list.length; i++) {
                        String temp = token.nextToken();
                        token1 = new StringTokenizer(temp);
                        token1.nextToken();
                        list[i] = Integer.parseInt(token1.nextToken());
                }
                return list;
        }

        /**
         * This method will return your highest score all time
         *
         * @return
         */
        public int highScore() {
                return Integer.parseInt(new StringTokenizer(da.readFile("src/dataAccess/HighScore.txt"), "\n").nextToken());
        }

        public void clearHighScore()
        {
        	da.writeFile("src/dataAccess/HighScore.txt", 0+"");
        }
        
        /**
         * This method will write your score into highScore file whether your
         * score is larger than current highScore
         *
         * @param score
         */
        public void updateHighScore(int score) {
                if (score > highScore()) {
                        da.writeFile("src/dataAccess/HighScore.txt", score + "");
                }
        }

        /**
         * This method will update your own selection key into GameControls.txt
         * file
         *
         * @param up
         * @param down
         * @param left
         * @param right
         * @param select
         * @param submit
         * @param clear
         */
        public void updateGameControls(int up, int down, int left, int right, int select, int submit, int clear) {
                String text = "MOVEUP: " + up + "\nMOVEDOWN: " + down + "\nMOVELEFT: " + left + "\nMOVERIGHT: " + right + "\nSELECTKEY: " + select + "\nSUBMITKEY: " + submit + "\nCLEARKEY: " + clear;
                da.writeFile("src/dataAccess/GameControls.txt", text);
        }

        /**
         * This method will update your game statics into GameStatics.txt file.
         *
         * @param maxLength
         * @param bestWord
         * @param charPerMin
         * @param totalTime
         * @param avgOfLength
         */
        public void updateGameStatics(int maxLength, String bestWord, int maxPoint, double charPerMin, int totalTime, double avgOfLength, int wordFound, int maxLevel) {
                String text = "MAXLENGTH: " + maxLength 
                		+ "\nBESTWORD: " + bestWord 
                		+ "\nMAXPOINT: " + maxPoint 
                		+ "\nCHARPERMIN: " + charPerMin 
                		+ "\nTOTALTIME: " + totalTime 
                		+ "\nAVGOFLENGTH: " + avgOfLength
                		+ "\nWORDFOUND: " + wordFound 
                		+ "\nMAXLEVEL: " + maxLevel;
                da.writeFile("src/dataAccess/GameStatics.txt", text);
        }

        /**
         * This method will return a list option of your last saved game to
         * continue playing
         *
         * @return
         */
        public String[] loadSavedGame() {
                String[] list = new String[6];
                StringTokenizer token = new StringTokenizer(da.readFile("src/dataAccess/SaveGame.txt"), "\n");
                for (int i = 0; i < list.length; i++) {
                        String temp = token.nextToken();
                        StringTokenizer token1 = new StringTokenizer(temp);
                        token1.nextToken();
                        list[i] = token1.nextToken();
                }
                return list;
        }

        /**
         * This method will save state of your game to play later.
         *
         * @param level
         * @param score
         * @param target
         * @param point
         * @param time
         * @param chars
         */
        public void saveGame(int level, int score, int target, int point, int time, String chars) {
                String text = "LEVEL: " + level + "\nSCORE: " + score + "\nTARGET: " + target + "\nPOINT: " + point + "\nTIME: " + time + "\nLIST: " + chars;
                da.writeFile("src/dataAccess/SaveGame.txt", text);
        }

        /**
         * This return the time formated like an electric watch with your input
         * time number
         *
         * @param time
         * @return
         */
        public String timeFormated(int time) {
                int minute = time / 60;
                int seconds = time - minute * 60;
                if (seconds < 10) {
                        return minute + ":0" + seconds;
                } else {
                        return minute + ":" + seconds;
                }
        }

        /**
         * This method will return a list that contains the word has the same
         * length with your input length
         *
         * @param length
         * @return
         */
        public ArrayList<String> getWordWithLength(int length) {
                ArrayList<String> list = new ArrayList<String>();

                for (String item : listWord) {
                        if (item.length() == length) {
                                list.add(item);
                        }
                }
                return list;
        }

        /**
         * This method only use for statics Form. It will format time with
         * hours, minutes and seconds like h:mm:ss
         *
         * @param time
         * @return
         */
        public String timeStaticsFormat(int time) {
                int hour = time / 3600;
                int minute = (time - hour * 3600) / 60;
                int seconds = time - (hour * 3600 + minute * 60);
                if (seconds < 10) {
                        if (minute < 10) {
                                return hour + ":0" + minute + ":0" + seconds;
                        }
                        return hour + ":" + minute + ":0" + seconds;
                } else {
                        if (minute < 10) {
                                return hour + ":0" + minute + ":" + seconds;
                        }
                        return hour + ":" + minute + ":" + seconds;
                }
        }

        
}
